package demo.spymemcached;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

import net.spy.memcached.MemcachedClient;

/**
 * 单实例多线程测试
 * 
 * @author hemj
 * @date 2016年11月16日
 */
public class SpySimpleTest {

	public static void main(String[] args) {
		new SpySimpleTest();
	}

	MemcachedClient mcc = null;

	SpySimpleTest() {
		try {
			List<InetSocketAddress> addrs = new ArrayList<InetSocketAddress>();
			addrs.add(new InetSocketAddress("192.168.85.3", 1121));
			addrs.add(new InetSocketAddress("192.168.85.128", 1121));
			mcc = new MemcachedClient(addrs);

			long start = System.currentTimeMillis();
			List<SetGet> threads = new ArrayList<SetGet>();
			for (int i = 0; i < 100; i++) {
				SetGet sg = new SetGet(i);
				threads.add(sg);
				sg.start();

			}
			for (int i = 0; i < threads.size(); i++) {
				try {
					threads.get(i).join();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			long end = System.currentTimeMillis();
			System.out.println("end-start: " + (end - start));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class SetGet extends Thread {
		int i = 0;

		SetGet(int i) {
			this.i = i;
		}

		public void run() {
			try {
				for (int j = 0; j < 100; j++) {
					// 存储数据
					String value = "Free Education" + j;
					mcc.set("runoob" + j, 60, value);
					// 输出值
					System.out
							.println(i + "   " + j + " >> " + value.equals(mcc.get("runoob" + j)));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
